package org.example.k8s_cloudplatform.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.annotation.Resource;
import org.example.k8s_cloudplatform.entity.Pod;
import org.example.k8s_cloudplatform.entity.res.Result;
import org.example.k8s_cloudplatform.service.PodAccessService;
import org.example.k8s_cloudplatform.service.PodService;
import org.example.k8s_cloudplatform.service.UserService;
import org.example.k8s_cloudplatform.utils.JwtUtil;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/pod")
public class PodController {
    @Resource
    private PodService podService;
    @Resource
    private UserService userService;

    @GetMapping("/viewMyPod")
    public Result viewMyPod(@RequestHeader("Token") String token,@RequestParam(defaultValue = "1") Integer pageNum,
                                         @RequestParam(defaultValue = "10") Integer pageSize) {
        Integer id= JwtUtil.IDParser(token);
        LambdaQueryWrapper<Pod> podWrapper= Wrappers.lambdaQuery();
        podWrapper.eq(Pod::getUserID,id);
        podWrapper.eq(Pod::getStatus,0);//用户仅看到已激活的pod

        Page<Pod> podPage=podService.page(new Page<>(pageNum, pageSize), podWrapper);
        return Result.success(podPage);
    }

    @GetMapping("/viewPodAccess")
    public Result viewPodAccess(@RequestHeader("Token") String token,@RequestParam(defaultValue = "1") Integer pageNum,
                            @RequestParam(defaultValue = "10") Integer pageSize) {
        int id= JwtUtil.IDParser(token);
        LambdaQueryWrapper<Pod> podWrapper= Wrappers.lambdaQuery();
        podWrapper.inSql(Pod::getId, "select dockerid from podaccess where userid = "+id);
        Page<Pod> podPage=podService.page(new Page<>(pageNum, pageSize), podWrapper);
        return Result.success(podPage);
    }
    @GetMapping("/viewActivePod")
    public Result viewActivePod(@RequestParam(defaultValue = "1") Integer pageNum,
                            @RequestParam(defaultValue = "10") Integer pageSize) {
        LambdaQueryWrapper<Pod> podWrapper= Wrappers.lambdaQuery();
        podWrapper.eq(Pod::getStatus,0);//仅看到已激活的pod

        Page<Pod> podPage=podService.page(new Page<>(pageNum, pageSize), podWrapper);
        return Result.success(podPage);
    }

    @GetMapping("/delete")
    public Result delete(@RequestHeader("Token") String token,@RequestParam Integer dockerID,
                            @RequestParam String password) {
        Integer userID= JwtUtil.IDParser(token);
        if(userService.checkPassword(userID,password)) {
            //验证成功开始删除
            return podService.delete(dockerID,0);
        }else
            return Result.fail("密码错误",null);
    }

}
